#!/usr/bin/env python
#
# Copyright 2008 Allen Hutchison (allen@hutchison.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# $URL: http://severedelays.googlecode.com/svn/trunk/appengine/lib/scrapers/uk/lon/tube_parser.py $
__version__ = "$Rev: 72 $"
__author__ = "Allen Hutchison (allen@hutchison.org)"

"""Parses the TFL live tube status page

This class parses the TFL live status page and returns an object that allows you
to get the status of the lines. 
"""

from lib.scrapers.scraper import Scraper

class TubeParser(Scraper):
  def __init__(self):
    url = 'http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html'
    Scraper.__init__(self, url=url, network='underground', geography='UK-LON', 
                     valueType='line')
    self.scrape()
    self.parse()

  def parse(self):
    self.status = {}
    block = self.soup.find('ul', id='lines')
    lines = block.findAll('li')
    for x in lines:
      line = x.h3.string.lower()
      if line == "h'smith &amp; city":
        line = "hammersmithandcity"
      elif line == "waterloo &amp; city":
        line = "waterlooandcity"
      
      self.status[line] = {}
      
      if "Good service" == x.div.string:
        self.status[line]['status'] = 'goodservice'
        self.status[line]['description'] = ''
      else:
        status = x.div.h4.string
        status = status.split(',');    
        description = ''
        for d in x.div.findAll('p'):
          if d.string is not None:
            description = description + d.string
        self.status[line]['description'] = description
        
        if "Minor delays" == status[0]:
          self.status[line]['status'] = 'minordelays'
        elif "Severe delays" == status[0]:
          self.status[line]['status'] = 'severedelays'
        elif "Part suspended" == status[0]:
          self.status[line]['status'] = 'partsuspended'
        elif "Suspended" == status[0]:
          self.status[line]['status'] = "suspended"
        elif "Part closure" == status[0]:
          self.status[line]['status'] = "partclosure"
        elif "Planned closure" == status[0]:
          self.status[line]['status'] = "plannedclosure"
      
    
  def getStatusValues(self):
    statusValues = []
    if self.status is not None:
      for line in self.status.keys():
        status = self.status[line]['status']
        description = str(self.status[line]['description'])
        statusValues.append(self.makeStatusValue(name=line, status=status, 
                            description=description))
    return statusValues
    